"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DDL-table
Case Name   : connect by + rownum/level查询表数据,比较结果
Create At   : 2023/01/09
Owner       : @zou_jialiang050
Description :
    1.建表并插入数据
    2.connect by + rownum/level查询表数据,比较结果
    3.清理环境
Expect      :
    1.成功
    2.查询结果一致
    3.成功
History     :
"""
import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Table(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.table = 't_ddl_table_0315'

    def test_table(self):
        text = '-----step1:建表并插入数据 expect:成功-----'
        self.logger.info(text)
        sql_cmd = f"drop table if exists {self.table};" \
            f"create table {self.table}(c_int1 int, c_int2 int, c_int3 int, " \
            f"c_int4 int);" \
            f"insert into {self.table} values (1,2,3,5),(2,3,4,4),(3,4,4,4);"
        self.logger.info(sql_cmd)
        msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(msg)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, msg, text +
                      '执行失败')
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, text +
                      '执行失败')
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败' + text)

        text = '-----step2:connect by + rownum/level查询表数据,比较结果 ' \
               'expect:查询结果一致-----'
        self.logger.info(text)
        sql_cmd = f"select * from {self.table} start with a=1 connect by " \
            f"prior b=a and (1=1 or rownum=1);"
        self.logger.info(sql_cmd)
        result1 = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(result1)
        sql_cmd = f"select * from {self.table} start with a=1 connect by " \
            f"prior b=a;"
        self.logger.info(sql_cmd)
        result2 = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(result2)
        result1_split = str(result1.splitlines()[-2]).strip()
        result2_split = str(result2.splitlines()[-2]).strip()
        self.assertEqual(result1_split, result2_split, '执行失败' + text)

    def tearDown(self):
        text = '-----step3:清理环境;expect:成功-----'
        self.logger.info(text)
        clean_cmd = f"drop table {self.table};"
        self.logger.info(clean_cmd)
        clean_msg = self.sh_primy.execut_db_sql(clean_cmd)
        self.logger.info(clean_msg)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, clean_msg,
                      '执行失败' + text)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
